/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.hibernate.dao;

import com.cashmate.hibernate.pojo.Requirements;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;

/**
 *
 * @author Lee
 */
public class RequirementDAO extends DAO {

    public RequirementDAO() {
        super();
    }

    //BASIC DAO METHODS
    @SuppressWarnings("unchecked")
    public List<Requirements> getAll() throws Exception {
        List<Requirements> a = (List<Requirements>) getSession().createQuery("from Requirements a").setCacheable(true).list();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public Requirements get(int id) throws Exception {
        Requirements a = (Requirements) getSession().createQuery("from Requirements a where a.requirementid=:id").setParameter("id", id).setCacheable(true).uniqueResult();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public void update(Requirements a) throws Exception {
        try {
            begin();
            getSession().update(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void insert(Requirements a) throws Exception {
        try {
            begin();
            getSession().save(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void delete(int id) throws Exception {
        try {
            begin();
            getSession().delete(get(id));
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public List<Requirements> findByCriteria(DetachedCriteria dc, int from, int size) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setFirstResult(from);
        criteria.setMaxResults(size);
        criteria.setCacheable(true);
        List<Requirements> list = criteria.list();
        flush();
        return list;
    }

    @SuppressWarnings("unchecked")
    public int countByCriteria(DetachedCriteria dc) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setCacheable(true);
        criteria.setProjection(Projections.rowCount());
        int count = ((Integer) criteria.list().get(0)).intValue();
        flush();
        return count;
    }
    
    //OTHER METHODS
    @SuppressWarnings("unchecked")
    public Requirements getByRequirementName(String reqname) throws Exception {
        Requirements r = (Requirements) getSession().createQuery("from Requirements r where r.requirementname=:name").setParameter("name", reqname).setMaxResults(1).setCacheable(true).uniqueResult();
        flush();
        return r;
    }

    @SuppressWarnings("unchecked")
    public boolean hasDuplicate(String requirement) throws Exception {
        Requirements e = (Requirements) getSession().createQuery("from Requirements b where b.requirementname=:name").setParameter("name", requirement).setCacheable(true).uniqueResult();
        flush();
        if (e == null) {
            return false;
        }
        return true;
    }

    @SuppressWarnings("unchecked")
    public List<Requirements> getActiveRequirements() throws Exception{
        //int i = 1;
        List<Requirements> reqs = (List<Requirements>) getSession().createQuery("from Requirements r where r.isactive=true").setCacheable(true).list();
        flush();
        return reqs;
    }
}
